package cn.j2ee.chapter2.web.controller;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.validation.BindException;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import cn.j2ee.chapter2.web.model.DataBinderTestModel;
import cn.j2ee.chapter2.web.model.PhoneNumberModel;
import cn.j2ee.chapter2.web.model.UserModel;
import cn.j2ee.chapter2.web.model.editor.PhoneNumberEditor;
import cn.j2ee.chapter2.web.service.UserService;

/**
 * 
 * 2016年11月1日 上午10:26:07
 */
public class UserController extends MultiActionController {  
	
    //用户服务类  
    private UserService userService;  
    //逻辑视图名 通过依赖注入方式注入，可配置  
    private String createView;  
    private String updateView;  
    private String deleteView;  
    private String listView;  
    private String redirectToListView;  
  
    public UserService getUserService() {
		return userService;
	}
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	public String getCreateView() {
		return createView;
	}
	public void setCreateView(String createView) {
		this.createView = createView;
	}
	public String getUpdateView() {
		return updateView;
	}
	public void setUpdateView(String updateView) {
		this.updateView = updateView;
	}
	public String getDeleteView() {
		return deleteView;
	}
	public void setDeleteView(String deleteView) {
		this.deleteView = deleteView;
	}
	public String getListView() {
		return listView;
	}
	public void setListView(String listView) {
		this.listView = listView;
	}
	public String getRedirectToListView() {
		return redirectToListView;
	}
	public void setRedirectToListView(String redirectToListView) {
		this.redirectToListView = redirectToListView;
	}
	
	@Override
	protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {
		super.initBinder(request, binder);
		 //注册自定义的属性编辑器  
        //1、日期 
		DateFormat dataFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		CustomDateEditor customDateEditor  = new CustomDateEditor(dataFormat, true);
		//表示如果命令对象有Date类型的属性，将使用该属性编辑器进行类型转换  
		binder.registerCustomEditor(Date.class, customDateEditor);
		//自定义的电话号码编辑器 
		binder.registerCustomEditor(PhoneNumberModel.class, new PhoneNumberEditor());
//		binder.registerCustomEditor(DataBinderTestModel.class, (PropertyEditor) new DataBinderTestModelValidator());
	}
	
	@Override
	protected Object newCommandObject(Class<?> clazz) throws Exception {
		
		return super.newCommandObject(DataBinderTestModel.class);
	}
	public String create(HttpServletRequest request, HttpServletResponse response, UserModel user) {  
        if("GET".equals(request.getMethod())) {  
            //如果是get请求 我们转向 新增页面  
            return getCreateView();  
        }  
        userService.create(user);  
        //直接重定向到列表页面  
        return getRedirectToListView();  
    }  
    public ModelAndView update(HttpServletRequest request, HttpServletResponse response, UserModel user) {  
        if("GET".equals(request.getMethod())) {  
            //如果是get请求 我们转向更新页面  
            ModelAndView mv = new ModelAndView();  
            //查询要更新的数据  
            mv.addObject("command", userService.get(user.getUsername()));  
            mv.setViewName(getUpdateView());  
            return mv;  
        }  
        userService.update(user);  
        //直接重定向到列表页面  
        return new ModelAndView(getRedirectToListView());  
    }  
  
      
    public ModelAndView delete(HttpServletRequest request, HttpServletResponse response, UserModel user) {  
        if("GET".equals(request.getMethod())) {  
            //如果是get请求 我们转向删除页面  
            ModelAndView mv = new ModelAndView();  
            //查询要删除的数据  
            mv.addObject("command", userService.get(user.getUsername()));  
            mv.setViewName(getDeleteView());  
            return mv;  
        }  
        userService.delete(user);  
        //直接重定向到列表页面  
        return new ModelAndView(getRedirectToListView());  
    }     
      
    public ModelAndView list(HttpServletRequest request, HttpServletResponse response,
    		Object command) {  
//    	,BindException errors
//    	errors.reject("username.not.empty1", "用户名不能为空");
//    	logger.error(errors.getMessage());
    	//输出command对象看看是否绑定正确  
    	logger.info("command:"+command);
    	
    	ModelAndView mv = new ModelAndView();  
    	mv.addObject("dataBinderTest", command);
        mv.addObject("userList", userService.list());  
        mv.setViewName(getListView());  
        return mv;  
    }  
      
    //如果使用委托方式，命令对象名称只能是command  
    protected String getCommandName(Object command) {  
        //命令对象的名字 默认command  
        return "command";  
    }  
}  
